import type { RootObject } from '@/api/data'
// const baseUrl = 'http://10.4.1.52:8689'
let baseUrl = '/api'
if (import.meta.env.MODE.indexOf('development') > -1) {
  // 开发地址
  baseUrl = 'http://10.4.1.52:8689'
}

export const getData = (url: string) => {
  const fetchUrl = baseUrl + url
  return fetch(fetchUrl, { method: 'get' }).then((res) => {
    return res.json() as unknown as Promise<FetchResponse>
  })
}

export const getDataByBinchou = (url: string, parms = 0) => {
  return fetch(baseUrl + url + `?areaCode=${parms}`, { method: 'get' }).then((res) => {
    return res.json() as unknown as Promise<RootObject>
  })
}

interface FetchResponse extends Response {
  code: number
  msg: string | null
  data: FetchDataResponse
}

interface FetchDataResponse {
  normal: NormalInfoResponse
  incomeRate: IncomeRateResponse
  hospitalizationInfo: CommonMapResponse
  emergencyInfo: CommonMapResponse
  totalIncomeInfo: CommonMapResponse
  bedRateInfo: BadRateResponse
  geoInfo: GeoInfoResponse[]
}

export interface NormalInfoResponse {
  medicalIncome: number
  emergencyIncome: number
  emergencyIncomePerson: number
  emergencyMedicineIncome: number
  hospitalIncome: number
  hospitalMedicineIncome: number
  hospitalVisits: number
  discharges: number
  peopleSurgery: number
  bedNumber: number
  totalBedNumber: number
  employeesNumber: number
}

export interface IncomeRateResponse {
  examine: number
  treat: number
  test: number
  inspect: number
  surgery: number
  drug: number
  [anyKey: string]: number
}

export interface CommonMapResponse {
  area: string[]
  data: number[]
}

interface BadRateResponse {
  data: MapResponse[]
  time: string[]
}

export interface MapResponse {
  name: string
  value: number[]
}

export interface GeoInfoResponse {
  name: string
  value: number
}
